package com.henu.acmrating.interceptor;

import com.henu.acmrating.utils.JwtUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginINterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)){
            return true;
        }

        String token = request.getHeader("token");
        if(token == null) {
            response.setStatus(401);
            return false;
        }

        Integer userId = JwtUtils.verity(token);
        if(userId == 0) {
            response.setStatus(401);
            return false;
        }

        return true;
    }
}
